/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.beans.maintenance;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.easoftware.code.beans.maintenance.annotation.DataTableColumn;

/**
 *
 * @author Eliu
 */
public class DataTableController implements Serializable {

    private List<DataTableRow> rows;
    private Class objectTemplate;
    private List dataSource;
    private DataTableRow modelRow;
    private Map<Integer, DataTableColumn> fieldsConfig = new HashMap<>();

    public static void BindEntireRow() {
    }

    public void doBind() {
        Map<Integer, DataTableColumn> fields = new HashMap<>();
        for (Field fld : objectTemplate.getDeclaredFields()) {
            DataTableColumn colConfig = fld.getAnnotation(DataTableColumn.class);
            if (colConfig != null) {
                fieldsConfig.put(colConfig.order(), colConfig);
            }
        }
        modelRow = new DataTableRow();
        modelRow.setListBinded(dataSource);
        modelRow.setIndex(0);
        for (int i = 1; i < 8; i++) {
            try {
                if (fieldsConfig.containsKey(i)) {
                    Method method = modelRow.getClass().getMethod("setCol" + i, DataTableRowCell.class);
                    DataTableRowCell cell = new DataTableRowCell(modelRow);
                    cell.setListIndex(0);
                    cell.setAttribute(fieldsConfig.get(i).name());
                    cell.setTitle(fieldsConfig.get(i).title());
                    cell.setShow(true);
                    cell.setParent(modelRow);
                    method.invoke(modelRow, cell);
                    cell.doBind();
                }
            }
            catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
                Logger.getLogger(DataTableController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public DataTableRow getModelRow() {
        return modelRow;
    }

    public void setModelRow(DataTableRow modelRow) {
        this.modelRow = modelRow;
    }

    public Class getObjectTemplate() {
        return objectTemplate;
    }

    public void setObjectTemplate(Class objectTemplate) {
        this.objectTemplate = objectTemplate;
    }

    public List getDataSource() {
        return dataSource;
    }

    public void setDataSource(List dataSource) {
        this.dataSource = dataSource;
    }

    public List<DataTableRow> getRows() {
        if (rows == null || rows.isEmpty()) {
            try {
                rows = new LinkedList<>();
                rows.add(modelRow);
                for (int i = 1; i < dataSource.size(); i++) {
                    DataTableRow clone = new DataTableRow();
                    clone.setObjectModel(modelRow.getObjectModel());
                    clone.setListBinded(dataSource);
                    clone.setIndex(i);

                    for (int j = 1; j < 8; j++) {
                        Method method = modelRow.getClass().getMethod("setCol" + j, DataTableRowCell.class);
                        if (fieldsConfig.containsKey(j)) {
                            DataTableRowCell c = new DataTableRowCell(clone);
                            c.setAttribute(fieldsConfig.get(j).name());
                            c.setTitle(fieldsConfig.get(j).title());
                            c.setListIndex(i);
                            c.setValue(null);
                            c.setShow(true);
                            c.doBind();
                            method.invoke(clone, c);
                        }
                    }
                    rows.add(clone);
                }
            }
            catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
                Logger.getLogger(DataTableController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return rows;
    }

    public void setRows(List<DataTableRow> rows) {
        this.rows = rows;
    }
}
